package servlets;

import java.io.IOException;
import java.io.PrintWriter;
//import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Zeitungsabo
 */
public class Zeitungsabo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Zeitungsabo() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println(ServletUtilities.headWithTitle("Abo - Formular"));
		out.println(ServletUtilities.holeFormular());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		// Enumeration
		/*Enumeration<String> paramNamen = request.getParameterNames();
		while(paramNamen.hasMoreElements()) {
			String paramName = paramNamen.nextElement();
			String paramWerte[] = request.getParameterValues(paramName);
			out.println("<u>" + paramName + "</u><br />");
			for(int i = 0; i < paramWerte.length; i++)
				out.println(paramWerte[i]+"<br />");
			out.println("<br />");
		}
		out.println("</body></html>");*/

		// Formularausgabe
		String vorname = request.getParameter("vorname");
		String nachname = request.getParameter("nachname");
		String ort = request.getParameter("ort");
		if (vorname.isEmpty() | nachname.isEmpty() | ort.isEmpty()) {
			out.println(ServletUtilities.headWithTitle("Abo - Formular"));
			out.println(ServletUtilities.holeFormular(vorname, nachname, ort));
		} else {
			out.println(ServletUtilities.headWithTitle("Abo - Formulardaten"));
			out.println("<h2>Eingegebene Formulardaten</h2>");
			out.println(vorname + " " + nachname + " aus " + ort);
			out.println("</body></html>");
		}
	}

}
